import Vue from 'vue';
import App from './App';
import router from './router/router';
import store from './store/store';
import 'element-ui/lib/theme-chalk/index.css'
Vue.prototype.type = function (arg) {
  //判断类型返回 object array function string number undefined ....
  if (arg) return Object.prototype.toString.call(arg).slice(8, -1).toLowerCase();
};
const scientificToNumber = Vue.prototype.scientificToNumber = function(num_str) {
  //科学计数法
  num_str = num_str.toString();
  if (num_str.indexOf("+") != -1) {
    num_str = num_str.replace("+", "");
  }
  if (num_str.indexOf("E") != -1 || num_str.indexOf("e") != -1) {
    var resValue = "",
      power = "",
      result = null,
      dotIndex = 0,
      resArr = [],
      sym = "";
    var numStr = num_str.toString();
    if (numStr[0] == "-") {
      //如果为负数，转成正数处理，先去掉‘-’号，并保存‘-’.
      numStr = numStr.substr(1);
      sym = "-";
    }
    if (numStr.indexOf("E") != -1 || numStr.indexOf("e") != -1) {
      var regExp = new RegExp(
        "^(((\\d+.?\\d+)|(\\d+))[Ee]{1}((-(\\d+))|(\\d+)))$",
        "ig"
      );
      result = regExp.exec(numStr);
      if (result != null) {
        resValue = result[2];
        power = result[5];
        result = null;
      }
      if (!resValue && !power) {
        return false;
      }
      dotIndex = resValue.indexOf(".") == -1 ? 0 : resValue.indexOf(".");
      resValue = resValue.replace(".", "");
      resArr = resValue.split("");
      if (Number(power) >= 0) {
        var subres = resValue.substr(dotIndex);
        power = Number(power);
        //幂数大于小数点后面的数字位数时，后面加0
        for (var i = 0; i <= power - subres.length; i++) {
          resArr.push("0");
        }
        if (power - subres.length < 0) {
          resArr.splice(dotIndex + power, 0, ".");
        }
      } else {
        power = power.replace("-", "");
        power = Number(power);
        //幂数大于等于 小数点的index位置, 前面加0
        for (var i = 0; i < power - dotIndex; i++) {
          resArr.unshift("0");
        }
        var n = power - dotIndex >= 0 ? 1 : -(power - dotIndex);
        resArr.splice(n, 0, ".");
      }
    }
    resValue = resArr.join("");
    return sym + resValue;
  } else {
    return num_str;
  }
};
Vue.prototype.for = function (tar, callback) {
  switch (this.type(tar)) {
    case  'array':
      for (let index = 0, len = tar.length; index < len; index++)
        callback(tar[index], index);
      break;
    case 'object':
      for (let key in tar)
        callback(tar[key], key);
      break;
    case 'nodelist': //dom节点
      for (let index = 0, len = tar.length; index < len; index++)
        callback(tar[index], index);
      break;
  }
};
Vue.prototype.timeout = function () {
  if (arguments.length == 2) {
    let time = Number(arguments[1]) || null;
    let callback = arguments[0] instanceof Function ? arguments[0] : null;
    if (callback) {
      time = time || 100;
      let newTimeout = null;
      newTimeout = setTimeout(function () {
        callback();
        clearTimeout(newTimeout);
        newTimeout = null;
        time = null;
        callback = null;
      }, time)
    }
  }
};
Vue.prototype.accMul = (arg1, arg2) => {
  /*高精度乘法*/
  //len，保留小数长度
  let max = 0, s1 = '' + arg1, s2 = '' + arg2;
  try {
    max += s1.split(".")[1].length
  } catch (e) {
  }
  try {
    max += s2.split(".")[1].length
  } catch (e) {
  }

  let result = Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, max);
  result =  scientificToNumber(result);
  return result;
};
Vue.prototype.accMoreMul = function () {
  //多个高精度乘法
  let a = 1,
    thiz = this;
  for (var i in arguments) {
    a = thiz.accMul(a, arguments[i])
  }
  a = scientificToNumber(a);
  return a;
};
Vue.prototype.accSub = function (arg1, arg2) {
  //高精度减法
  let r1, r2, max, min;
  try {
    r1 = arg1.toString().split(".")[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = arg2.toString().split(".")[1].length
  } catch (e) {
    r2 = 0
  }
  max = Math.pow(10, Math.max(r1, r2));
  //动态控制精度长度
  min = (r1 >= r2) ? r1 : r2;
  let result = ((arg1 * max - arg2 * max) / max).toFixed(min)
  result = result == 'NaN' ? 0 : result;
  result = scientificToNumber(result);
  return  result
};
Vue.prototype.accDiv = function (arg1, arg2, len) {
  //高精度除法
  //sli_len 保留小数长度
  let t1 = 0, t2 = 0, r1, r2;
  try {
    t1 = arg1.toString().split(".")[1].length
  } catch (e) {
  }
  try {
    t2 = arg2.toString().split(".")[1].length
  } catch (e) {
  }
  // with (Math) {
  r1 = Number(arg1.toString().replace(".", ""));
  r2 = Number(arg2.toString().replace(".", ""));
  let result = (r1 / r2) * Math.pow(10, t2 - t1);
  if (len) {
    result = this.numSlice(result, len);
  }
  result = scientificToNumber(result);
  return result;
};
Vue.prototype.accAddMore = function () {
  //加法，多个值
  let accAdd = function (arg1, arg2) {
    let r1, r2, max;
    try {
      r1 = arg1.toString().split(".")[1].length
    } catch (e) {
      r1 = 0
    }
    try {
      r2 = arg2.toString().split(".")[1].length
    } catch (e) {
      r2 = 0
    }
    max = Math.pow(10, Math.max(r1, r2));
    return (arg1 * max + arg2 * max) / max
  };
  let args = arguments;
  let result = 0;
  if (args.length >= 2) {
    for (let i = 0; i < args.length; i++) {
      result = accAdd(result, String(args[i]));
    }
  }
  result = scientificToNumber(result);
  return result;
};
Vue.prototype.loading = function () {
  store.commit('isLoading', true); //全局公用显示加载动画方法： this.loading() 或者 store.commit('isLoading',true);
};
Vue.prototype.closeLoading = function () {
  store.commit('isLoading', false); //全局公用关闭加载动画方法： this.closeLoading() 或者 store.commit('isLoading',false);
};
Vue.prototype.numSlice = function (str, len) {
  //截取小数长度
  let result = String(str);
  if (len || typeof len == "number") {
    result = String(result).split('.');
    try {
      result = Number(result[0] + '.' + result[1].slice(0, len));
    } catch (e) {
      result = Number(result[0]);
    }
  }
  result = scientificToNumber(result);
  return result;
};
Vue.prototype.arrRemove = function (arr, delIndex) {
  //删除数组制定下标的元素
  var temArray = [];
  for (var i = 0; i < arr.length; i++) {
    if (i != delIndex) {
      temArray.push(arr[i]);
    }
  }
  return temArray;
};
Vue.prototype.addDaysFromNow = function (n) {
  var uom = new Date(new Date() - 0 + n * 86400000);
  var month = uom.getMonth() + 1 < 10 ? "0" + (uom.getMonth() + 1) : uom.getMonth() + 1;
  var days = uom.getDate() < 10 ? "0" + uom.getDate() : uom.getDate();
  var time = " 00:00:00";
  uom = uom.getFullYear() + "-" + month + "-" + days + time;
  return uom;
};
Vue.prototype.addDaysFromDesignDate = function (m, n) {
  var uom = new Date(m - 0 + n * 86400000);
  var month = uom.getMonth() + 1 < 10 ? "0" + (uom.getMonth() + 1) : uom.getMonth() + 1;
  var days = uom.getDate() < 10 ? "0" + uom.getDate() : uom.getDate();
  var hh = uom.getHours() < 10 ? "0" + uom.getHours() : uom.getHours();
  var mm = uom.getMinutes() < 10 ? "0" + uom.getMinutes() : uom.getMinutes();
  var ss = uom.getSeconds() < 10 ? "0" + uom.getSeconds() : uom.getSeconds();
  uom = uom.getFullYear() + "-" + month + "-" + days + " " + hh + ":" + mm + ":" + ss;
  return uom;
};
Vue.prototype.isNumber = function (val) {
  var regPos = /^\d+(\.\d+)?$/;
  var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
  if (regPos.test(val) || regNeg.test(val)) {
    return true;
  } else {
    return false;
  }
};
Vue.prototype.PromiseAll = function (https, callback) {
  //封装Promise.all
  if (https && this.type(https) === 'array' && https.length > 0) {
    this.loading();
    Promise.all(https).then(res => {
      this.closeLoading();
      if (callback) callback(res);
    }).catch(err => {
      this.closeLoading();
      if (callback) callback(err || false);
    });
  }
};

new Vue({
  el: '#app',
  router,
  store,
  components: {App},
  template: '<App/>'
});
